package com.sc.insert;

import android.content.ContentResolver;
import android.content.ContentUris;
import android.content.ContentValues;
import android.content.Intent;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.provider.Telephony;
import androidx.appcompat.app.AppCompatActivity;
import android.text.method.ScrollingMovementMethod;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import java.text.SimpleDateFormat;
import java.util.Date;

public class SmsActivity extends AppCompatActivity {
    private EditText et_sms_id;
    private EditText et_sms_number;
    private EditText et_sms_content;
    private EditText et_sms_type;
    private TextView tv_sms_view;
    private Button bt_insert;
    private Button bt_edit_default;
    private Button bt_change_default;
    private String defaultSmsPkg;
    private String mySmsPkg;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_sms);

        et_sms_id = findViewById(R.id.et_sms_id);
        et_sms_number = findViewById(R.id.et_sms_number);
        et_sms_content = findViewById(R.id.et_sms_content);
        et_sms_type = findViewById(R.id.et_sms_type);
        tv_sms_view = findViewById(R.id.tv_sms_view);
        bt_edit_default = findViewById(R.id.bt_edit_default);
        bt_change_default = findViewById(R.id.bt_change_default);

        tv_sms_view.setMovementMethod(ScrollingMovementMethod.getInstance()); //设置滚动
        defaultSmsPkg = Telephony.Sms.getDefaultSmsPackage(this);
        mySmsPkg = this.getPackageName();

        bt_edit_default.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (!defaultSmsPkg.equals(mySmsPkg)) {
//            如果这个App不是默认的Sms App，则修改成默认的SMS APP
//            因为从Android 4.4开始，只有默认的SMS APP才能对SMS数据库进行处理
                    Intent intent = new Intent(Telephony.Sms.Intents.ACTION_CHANGE_DEFAULT);
                    intent.putExtra(Telephony.Sms.Intents.EXTRA_PACKAGE_NAME, mySmsPkg);
                    startActivity(intent);
                }
            }
        });

        bt_insert = findViewById(R.id.bt_insert);
        bt_insert.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mySmsPkg.equals(Telephony.Sms.getDefaultSmsPackage(SmsActivity.this))) {
                    System.out.println("My App is default SMS App.");
                    String number = et_sms_number.getText().toString();
                    String content = et_sms_content.getText().toString();
                    String type = et_sms_type.getText().toString();
                    String id = et_sms_id.getText().toString();
                    // 对短信数据库进行处理
                    ContentResolver resolver = getContentResolver();
                    ContentValues values = new ContentValues();
                    values.put("address", number);
                    values.put("type", type);
                    values.put("date", System.currentTimeMillis());
                    values.put("body", content);
                    values.put("sim_id", id);

                    Uri uri = resolver.insert(Telephony.Sms.CONTENT_URI, values);
                    if (uri != null) {
                        long uriId = ContentUris.parseId(uri);
                        System.out.println("uriId " + uriId);
                    }

                    bt_change_default.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            changeDeault();
                        }
                    });

//                    打印出收件箱里的最新5条短信
                    Cursor cursor = getContentResolver().query(Telephony.Sms.CONTENT_URI, null, null, null, null);
                    String msg = "";
                    while ((cursor.moveToNext()) &&
                            (cursor.getPosition() < 5)) {
                        int dateColumn = cursor.getColumnIndex("date");
                        int phoneColumn = cursor.getColumnIndex("address");
                        int smsColumn = cursor.getColumnIndex("body");

                        System.out.println("count " + cursor.getCount() + " position " + cursor.getPosition());
//                        把从短信中获取的时间戳换成一定格式的时间
                        SimpleDateFormat sfd = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
                        Date date = new Date(Long.parseLong(cursor.getString(dateColumn)));
                        String time = sfd.format(date);
                        msg = msg + time + " " + cursor.getString(phoneColumn) + ":" + cursor.getString(smsColumn) + "\n";
                        tv_sms_view.setText(msg);
                    }
                } else {
                    Toast.makeText(SmsActivity.this, "Sorry,the App is not default Sms App.",
                            Toast.LENGTH_LONG).show();
                }
            }
        });
    }

    private void changeDeault() {
        //对短信数据库处理结束后，恢复原来的默认SMS APP
        Intent intent = new Intent(Telephony.Sms.Intents.ACTION_CHANGE_DEFAULT);
        intent.putExtra(Telephony.Sms.Intents.EXTRA_PACKAGE_NAME, defaultSmsPkg);
        startActivity(intent);
    }

    @Override
    protected void onPause() {
        changeDeault();
        super.onPause();
    }
}
